<!DOCTYPE html>
<html lang="zh">
<head>
  <meta charset="UTF-8">
  <title>短信验证码登录</title>
  <style>
    body {
      font-family: "Segoe UI", sans-serif;
      background-color: #f0f6ff;
      margin: 0;
      padding: 0;
    }

    .container {
      max-width: 500px;
      margin: 120px auto;
      background-color: #ffffff;
      padding: 30px;
      border-radius: 12px;
      box-shadow: 0 8px 24px rgba(0, 0, 0, 0.08);
    }

    h2 {
      text-align: center;
      color: #1e2f45;
      margin-bottom: 30px;
    }

    .form-group {
      margin-bottom: 20px;
    }

    label {
      display: block;
      font-weight: 600;
      margin-bottom: 8px;
    }

    input {
      width: 100%;
      padding: 10px;
      border: 1px solid #ccd6e0;
      border-radius: 6px;
      background-color: #eef4ff;
    }

    .input-group {
      display: flex;
      gap: 10px;
    }

    .input-group input {
      flex: 1;
    }

    .btn {
      padding: 12px;
      background-color: #dbe7ff;
      border: none;
      border-radius: 6px;
      font-weight: bold;
      color: #2b3f5c;
      cursor: pointer;
      transition: background-color 0.2s;
    }

    .btn:hover {
      background-color: #c3d8ff;
    }

    .btn-block {
      width: 100%;
      margin-top: 10px;
    }

    .code-msg {
      margin-top: 15px;
      color: green;
      font-size: 14px;
      text-align: center;
    }

    .link {
      text-align: center;
      margin-top: 20px;
    }

    .link a {
      color: #337ab7;
      text-decoration: none;
    }

    .link a:hover {
      text-decoration: underline;
    }
  </style>
</head>
<body>

<div class="container">
  <h2>短信验证码登录</h2>
  <form id="sms-login-form">
    <div class="form-group">
      <label>手机号：</label>
      <input type="text" name="phone" required>
    </div>

    <div class="form-group">
      <label>验证码：</label>
      <div class="input-group">
        <input type="text" name="code" required>
        <button type="button" class="btn" onclick="sendCode()">获取验证码</button>
      </div>
    </div>

    <button type="submit" class="btn btn-block">登录</button>

    <div id="debug-code" style="color:green; margin-top:10px;"></div>
  </form>
</div>

<script>
function sendCode() {
  const phone = document.querySelector('input[name="phone"]').value.trim();
  if (!phone) {
    alert("请输入手机号！");
    return;
  }

  fetch(`/send_sms_code?phone=${phone}&mode=login`)
    .then(res => res.text())
    .then(text => {
      document.getElementById("debug-code").innerText = text;
    })
    .catch(err => {
      document.getElementById("debug-code").innerText = "发送失败：" + err;
    });
}

document.getElementById('sms-login-form').addEventListener('submit', function(e) {
  e.preventDefault();
  const formData = new FormData(this);
  fetch('/login_sms', {
    method: 'POST',
    body: formData
  })
  .then(res => res.json())
  .then(data => {
    if(data.status === 'success'){
      alert(data.message);
      window.location.href = data.redirect_url;
    } else {
      alert(data.message);
    }
  })
  .catch(() => alert('请求失败，请稍后重试'));
});
</script>

</body>
</html>
